/**
 * 
 */
package edu.uwm.nlp.jude.internal;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import org.pdfbox.cos.COSDocument;
import org.pdfbox.pdfparser.PDFParser;
import org.pdfbox.pdmodel.PDDocument;
import org.pdfbox.util.PDFTextStripper;

import edu.uwm.nlp.jude.util.ProjUtil;

/**
 * @author qing
 *
 * Jun 15, 2009
 */
public class FileProcessor implements DocumentHandler {
	
	
	public FileProcessor()
	{
	}
	
	public void processPDFArticle(String fileLoc, String save, boolean isSave)
	{
		FileWriter writer = null;
		try {
			String text = getDocumentText(fileLoc);
			if(text == null) return;
			ReferenceAnalyser ra = new ReferenceAnalyser(text);
			ArrayList<ReferenceEntity> rList = ra.analyzeReference();
			File f = new File(fileLoc);
			if(rList != null && isSave)
			{
				writer = new FileWriter(save+f.getName().substring(0, f.getName().lastIndexOf('.'))+".txt");
				writer.append(rList.toString());
				writer.append("\ntotal split="+rList.size()+"\ntotal spot="+ProjUtil.getTotalSpot(rList)+"\n");
				writer.close();
			}
		} catch (DocumentHandlerException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	/**
	 * This method is used for web app. Particularly for single pdf processing.
	 * @param text
	 * @return
	 */
	public ArrayList<ReferenceEntity> getFullCitations(String text)
	{
		ArrayList<ReferenceEntity> rList = null;
		if(text!=null)
		{
			ReferenceAnalyser ra = new ReferenceAnalyser(text);
			rList = ra.analyzeReference();
		}
		return rList;
	}
	
	public String getDocumentText(InputStream is)throws DocumentHandlerException
	{
		COSDocument cosDoc = null;
		try {
			if(is!=null)
				cosDoc = parseDocument(is);
		} catch (IOException e) {
			closeCOSDocument(cosDoc);
			throw new DocumentHandlerException("Cannot parse PDF document");
		}
		
		PDFTextStripper stripper;
		String docText = null;
		try {
			stripper = new PDFTextStripper();
			PDDocument pdDoc = new PDDocument(cosDoc);
			docText = stripper.getText(pdDoc);
									// get the plain text of the pdf file
			closeCOSDocument(cosDoc);
			pdDoc.close();
		} catch (IOException e) {
			e.printStackTrace();
			closeCOSDocument(cosDoc);
			return docText;
		}
		
		
		return docText;
	}
	
	public String getDocumentText(String fileLoc) throws DocumentHandlerException
	{
		FileInputStream is = null;
		try {
			is = new FileInputStream(fileLoc);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		
		COSDocument cosDoc = null;
		try {
			if(is!=null)
				cosDoc = parseDocument(is);
		} catch (IOException e) {
			closeCOSDocument(cosDoc);
			throw new DocumentHandlerException("Cannot parse PDF document");
		}
		
		PDFTextStripper stripper;
		String docText = null;
		try {
			stripper = new PDFTextStripper();
			PDDocument pdDoc = new PDDocument(cosDoc);
			docText = stripper.getText(pdDoc);
									// get the plain text of the pdf file
			closeCOSDocument(cosDoc);
			pdDoc.close();
		} catch (IOException e) {
			e.printStackTrace();
			closeCOSDocument(cosDoc);
			return docText;
		}
		
		
		return docText;
	}
	
	private static COSDocument parseDocument(InputStream is) throws IOException
	{
		PDFParser parser = new PDFParser(is);
		parser.parse();
		return parser.getDocument();
	}
	
	private void closeCOSDocument(COSDocument cosDoc)
	{
		if(cosDoc!=null)
			try {
				cosDoc.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
	}
	
	
	
	public static void main(String[] args)
	{
		if(args.length!=3){
			System.out.println("Test pdf/pdfs\n--pdf-location-or-dir --save --isSave");
			return;
		}
			
		
		
//			FileProcessor entry = new FileProcessor();
//			File dir = new File(args[0]);
//			File[] files = dir.listFiles();
//			int size = files.length;
//			for(int i=0; i<size; i++)
//			{
//				entry.processPDFArticle(files[i].getAbsolutePath(), args[1],Boolean.parseBoolean(args[2]));
//			}
		
		FileProcessor fprocessor = new FileProcessor();
		fprocessor.processPDFArticle(args[0], args[1], Boolean.parseBoolean(args[2]));
	}
}
